package com.briup.jdbc;

import java.sql.*;

public class StatementCompareTime {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        PreparedStatement ps = null;

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "bttc2", "bttc2");
            statement = connection.createStatement();
            String sql = "insert into team values(seq_test1.nextval,'tom')";
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                statement.addBatch(sql);
            }
            statement.executeBatch();
            long end = System.currentTimeMillis();
            System.out.println("statement耗时：" + (end - start));
//            创建preparedStatement对象 同时就要指定sql
            ps = connection.prepareStatement("insert into TEAM  values (SEQ_TEST1.nextval,?)");
            start = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                ps.setString(1, "adq");
                ps.addBatch();
            }
            ps.executeBatch();
            end = System.currentTimeMillis();
            System.out.println("PreparedStatement耗时：" + (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                if (connection != null)
                    connection.close();
                if (statement!=null)
                    statement.close();
                if (ps!=null)
                    ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
